<script>
$(document).ready(function(){
		//
	var last_value = 0;
	$('td.cell_update_unit_change').click(function(){
			//Luu lai gia tri truoc do.
		last_value = $.trim( $(this).text() );	//bo di khoan trang...
		
			//Tao the input
		var input_tag = $("<input />").attr({type: 'text', name : 'update_unit_change'}).css({"width":'100%', 'text-align' : 'right'}).val(last_value);
		
			//Dua the input vao td.
		$(this).html(input_tag);
		input_tag.focus().select();
	});

	$(document.body).delegate("input[name='update_unit_change']", 'blur', function(){
		callAjax($(this));
	});

	$(document.body).delegate("input[name='update_unit_change']", 'keydown', function(event){
		 if (event.keyCode == '13') {
			 callAjax($(this));
		 }
	});

	function callAjax(self)
	{
		var value = parseFloat(self.val().replace(/\./g, '').replace(/,/g, '.'));
		
		if((value == null) || isNaN(value) || (value.toString() == '') || (value < 0)){
			self.val(last_value);
		}else{
			var unit_change_code = self.parent().attr('unit_change_code');
			var unit_change_unit_code = self.parent().attr('unit_change_unit_code');
			 $.ajax ({
			   type: 'POST',
			   url: '<?php echo $this->url(array("action"=>"ajax-update", 'controller'=>'index', 'module'=>'unit-change'), NULL, TRUE) ;?>',
			   data: {"unit_change_unit_code":unit_change_unit_code, "unit_change_code":unit_change_code, "unit_change_rate":value},
			   success: function(msg){
					$(self).parent().html(number_format(value, 2, ',', '.' ) + '&nbsp;');	   	
			   }
			})
		}
	}
});	
</script>

<?php $this->placeholder('toolbar')->captureStart();?>
<?php echo $this->partial('index/toolbar/divider.phtml', 'admin') ?>
<?php echo $this->partial('index/toolbar/new.phtml', 'admin', array($this->url(array('action' => 'insert')))) ?>
<?php //echo $this->partial('index/toolbar/edit.phtml', 'admin', array($this->url(array('action' => 'update')))) ?>
<?php echo $this->partial('index/toolbar/divider.phtml', 'admin') ?>
<?php echo $this->partial('index/toolbar/trash.phtml', 'admin', array($this->url(array('action' => 'delete')))) ?>
<?php $this->placeholder('toolbar')->captureEnd();?>

<form id="adminForm" name="adminForm" method="post" action="">
	<div class="clr"> </div>
	<div style='width:100%;overflow-x: scroll'>
		<table class="adminlist">
			<thead>			
				<tr>
					<?php echo $this->partial('index/manage/checkbox-all.phtml', 'admin'); ?>
					<th >
						<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>$this->captions,'column'=>'unit_change_code')); ?>
					</th>	
					<th >
						<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>$this->captions,'column'=>'unit_change_name')); ?>
					</th>			
					<?php foreach ($this->units as $unit){?>
					<th><?php echo $unit->unit_code;?></th>
					<?php }?>				
					<th width="65px">Quản lý</th>
				</tr>
			</thead>
			<tbody>		
				<?php $j=1; ?>
				<?php foreach($this->array as $i => $row): ?>
				<tr class='row<?php echo ($j++)%2 ?>'>					
					<?php echo $this->partial('index/manage/checkbox.phtml', 'admin', array($i)) ?>
					<td class="center is_code"><?php echo $i?></td>
					<td ><?php echo $row['unit_change_name']?></td>
					<?php foreach ($this->units as $key => $unit){?>
					<td style='padding:0px;width:100px' class='right cell_update_unit_change<?php echo $row[$unit->unit_code]>0?" is_code":""?>' unit_change_unit_code='<?php echo $unit->unit_code ;?>' unit_change_code='<?php echo $i ;?>'>
						<?php echo $this->partial('index/format/float.phtml', 'admin', array($row[$unit->unit_code])) ;?>&nbsp;
					</td>
					<?php }?>					
					<td class="center">				
						<?php echo $this->partial("index/manage/edit.phtml","admin", array($this->url(array('action' => 'update','unit_change_code'=>$i)))); ?>
						<?php echo $this->partial("index/manage/trash.phtml","admin", array($this->url(array('action' => 'delete')))); ?>	
					</td>
				</tr>
				<?php endforeach; ?>
			</tbody>
			<tfoot>			
				<tr>
					<th></th>
					<th >
						<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>$this->captions,'column'=>'unit_change_code')); ?>
					</th>	
					<th >
						<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>$this->captions,'column'=>'unit_change_name')); ?>
					</th>				
					<?php foreach ($this->units as $unit){?>
					<th ><?php echo $unit->unit_code;?></th>
					<?php }?>				
					<th width="65px">Quản lý</th>
				</tr>
			</tfoot>			
		</table>
	</div>
	<table class="adminlist">
		<tfoot>
			<tr>
				<td colspan="100"><div class="container"><?php //echo $this->paginationControl($this->paginator);?></div></td>
			</tr>
		</tfoot>
	</table>
</form>

